package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.UsersBank;
import com.serkol.ams.mapper.UsersBankMapper;
import com.serkol.ams.service.UsersBankService;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import com.serkol.utils.LangValidUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【users_bank】的数据库操作Service实现
 * @createDate 2022-12-08 00:38:04
 */
@Service
public class UsersBankServiceImpl extends ServiceImpl<UsersBankMapper, UsersBank>
        implements UsersBankService {

    @Override
    public MyPageOld findByPageList(int pageSize, int current) {
        QueryWrapper<UsersBank> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("ID");
        Page<UsersBank> page = new Page<>(current, pageSize);
        Page<UsersBank> mapPage = this.baseMapper.selectPage(page, queryWrapper);
        return MyPageOld.parsePage(mapPage);

    }


    @Override
    public List<Map<String, Object>> findBanksMyMiniList() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<UsersBank> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(UsersBank::getUserId, loginLog.getUserId()).eq(UsersBank::getState, 0);
        // queryWrapper.langName(UsersBank::getName, Banks::getNameUg).as("name").select(Banks::getId, Banks::getStatus, Banks::getType,Banks::getColor,Banks::getIcon).ne(Banks::getStatus,1);
        queryWrapper.select(UsersBank::getId, UsersBank::getPhone, UsersBank::getState, UsersBank::getType,UsersBank::getStatus).select(UsersBank::getName).as("userName").select(UsersBank::getCardNo).as("cardNo").select(UsersBank::getBankId).as("bankId");
        Map<String, Object> userBankQueary = new HashMap<>();
        userBankQueary.put("ID", "BANK_ID");
        queryWrapper.otherTableColmuns("banks", LangValidUtils.getValid("ug", "NAME_UG", "NAME"), userBankQueary).as("name");
        // queryWrapper.otherTableColmuns("banks", "CARD_NO", userBankQueary).as("cardNo");
        queryWrapper.otherTableColmuns("banks", "TYPE", userBankQueary).as("type");
        queryWrapper.otherTableColmuns("banks", "STATUS", userBankQueary).as("status");
        queryWrapper.otherTableColmuns("banks", "COLOR", userBankQueary).as("color");
        queryWrapper.otherTableColmuns("banks", "ICON", userBankQueary).as("icon");


        List<Map<String, Object>> maps = this.listMaps(queryWrapper);

        return maps;
    }

    @Override
    public boolean byDelete(long id) {
        int i = this.baseMapper.deleteById(id);
        return i > 0 ? true : false;
    }

    @Override
    public Map<String, Object> findById(Long aLong) {
        MyLambdaQueryWrapper<UsersBank> queryWrapper = new MyLambdaQueryWrapper<>();

        // queryWrapper.langName(UsersBank::getName, Banks::getNameUg).as("name").select(Banks::getId, Banks::getStatus, Banks::getType,Banks::getColor,Banks::getIcon).ne(Banks::getStatus,1);
        queryWrapper.eq(UsersBank::getId, aLong).select(UsersBank::getId, UsersBank::getPhone, UsersBank::getAddress, UsersBank::getState, UsersBank::getType,UsersBank::getStatus,UsersBank::getCardNo,UsersBank::getBankId).select(UsersBank::getName).as("userName");
        queryWrapper.picture(UsersBank::getImageId).as("image");
        Map<String, Object> userBankQueary = new HashMap<>();
        userBankQueary.put("ID", "BANK_ID");
        queryWrapper.otherTableColmuns("banks", "name", userBankQueary).as("name");
        Map<String, Object> map = this.getMap(queryWrapper);

        return map;
    }

    @Override
    public Map<String, Object> getInfo(long id) {
        MyLambdaQueryWrapper<UsersBank> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(UsersBank::getId, id).select(UsersBank::getId, UsersBank::getPhone, UsersBank::getAddress, UsersBank::getState, UsersBank::getType,UsersBank::getStatus).select(UsersBank::getName).as("userName").select(UsersBank::getCardNo).as("cardNo").select(UsersBank::getBankId).as("bankId");
        queryWrapper.picture(UsersBank::getImageId).as("image");
        queryWrapper.last("LIMIT 1");
        Map<String, Object> userBankQueary = new HashMap<>();
        userBankQueary.put("ID", "BANK_ID");
        queryWrapper.otherTableColmuns("banks", LangValidUtils.getValid("ug", "NAME_UG", "NAME"), userBankQueary).as("name");
        // queryWrapper.otherTableColmuns("banks", "CARD_NO", userBankQueary).as("cardNo");
        queryWrapper.otherTableColmuns("banks", "TYPE", userBankQueary).as("type");
        queryWrapper.otherTableColmuns("banks", "STATUS", userBankQueary).as("status");
        queryWrapper.otherTableColmuns("banks", "COLOR", userBankQueary).as("color");
        queryWrapper.otherTableColmuns("banks", "ICON", userBankQueary).as("icon");
        Map<String, Object> map = this.getMap(queryWrapper);
        return map;
    }
}




